package sip_core

import (
	"github.com/sirupsen/logrus"
)

// MessageNotify 心跳包xml结构
type MessageNotify struct {
	CmdType  string `xml:"CmdType"`
	SN       int    `xml:"SN"`
	DeviceID string `xml:"NvrDeviceSipId"`
	Status   string `xml:"Status"`
	Info     string `xml:"Info"`
}

func SipMessageKeepalive(u NVRDevices, body string) error {
	message := &MessageNotify{}
	if err := XMLDecode([]byte(body), message); err != nil {
		logrus.Errorln("Message Unmarshal xml err:", err, "body:", body)
		return err
	}
	// update := M{}
	if message.Status == "OK" {
		// update["active"] = time.Now().Unix()
		_activeDevices.Store(u.DeviceID, u)
	} else {
		// update["active"] = -1
		_activeDevices.Delete(u.DeviceID)
	}
	// go notify(notifyUserAcitve(u.NvrDeviceSipId, message.Status))
	// return dbClient.Update(userTB, M{"DeviceID": u.NvrDeviceSipId}, M{"$set": update})
	return nil
}
